package com.cangmaomao.m_mePen.presenter

import com.cangmaomao.lib.utils.DateUtils.fromDate
import com.cangmaomao.m_mePen.bean.SortListBean
import com.cangmaomao.m_mePen.contract.FileListContract
import java.util.*

class FileListDetailsPresenter(val view: FileListContract.DetailsView) : FileListContract.DetailsPresenter {

    init {
        view.setPresenter(this)
    }

    override fun start() {
        readFiles()
    }


    override fun readFiles() {
        val files = view.readLocalFile()
        if (!files.isEmpty()) {
            val list = mutableListOf<SortListBean>()
            files.forEach {
                val childList = it.listFiles()
                if (!childList.isEmpty()) {
                    childList.forEach {
                        val cal = Calendar.getInstance()
                        cal.timeInMillis = it.lastModified()
                        val type = if (it.name.contains("mp4")) 2 else 1
                        list.add(SortListBean(fromDate(cal), it.path,  it.path, "", type, false, false))
                    }
                }
            }
            view.showLocalFile(list)
        }
    }


}